
Ext.define('FrontSuite.view.FileTreePanel', {
    extend:       'Ext.tree.Panel',  // defines the superclass. results in class inheritance.

    id           : 'file-tree-panel',
    scroll       : false,
    bodyStyle    : {border:0},
    useArrows    : true,
    rootVisible  : false,
    expNodes     : [],
    sortOnDrop   : true,

    store        : null,

    constructor: function (id) {
        if (id) {
            this.id = id;
        }
        var testMode = FrontSuite.options.test_mode;

        // screen tree store
        this.store = Ext.create('Ext.data.TreeStore', {
            storeId  : 'file-tree-store',
            autoLoad : true,
            //autoSync : true,
            //model    : 'ActionTreeModel',
            proxy : {
                type : 'ajax',
                url  : FrontSuite.options.apps_urldir + '/sitefront/front-framework/api.php/service/ext/filetree',
                extraParams: { op : 'getTree' },
                actionMethods : 'GET'
            },
            root: {
                text     : 'Files',
                id       : 'src',
                expanded : true
            }
        });

        this.superclass.constructor.call(this, id);
        return(this);
    },

    listeners    : {
        itemcontextmenu: function(view, rec, node, index, e) {
            console.log(this);
            e.stopEvent();
            fileTreeContextMenu.showAt(e.getXY());
            FrontSuite.session.lastTaskDocumentIdClicked       = rec.data.id;
            FrontSuite.session.lastTaskDocumentParentIdClicked = rec.parentNode.data.id;
            FrontSuite.session.lastTaskDocumentLeafClicked     = rec.data.leaf;
            return false;
        },
        load : function () {
            // expand any nodes that were expanded before a reload
            var rootNode = this.getRootNode();
            for (var idx = 0; idx < this.expNodes.length; idx++) {
                if ( (node = rootNode.findChild('id', this.expNodes[idx], true)) ){ // renamed nodes will no longer exist
                    node.expand(false);
                }
            }
        },
    },

    viewConfig : {
        plugins    : {
            ptype : 'treeviewdragdrop'
        },
        //sortOnDrop : true,
        listeners  : {
            beforedrop : function(newParentHtml , oldParent, newParent, operation, eOpts) {
                if (screenTreeStore.isLoading()) {
                    console.log("need to handle is loading case");
                }
                else {
                    //for (var i = 0; i < data.records.length; i++) {
                        var from  = oldParent.records[0].internalId;
                        var to    = (newParent.raw.id == 'src') ? '.' : newParent.internalId;
                        var isDir = (oldParent.records[0].raw.leaf == undefined);

                        // TODO: figure out how to disable inserts with drag and drop
                        if (operation != 'append'){
                            if ( isDir ){
                                if ( /\//.test(to) )
                                    to = to.replace(/[^\/]+[\/]{0,1}$/,"");
                                else
                                    to = ".";
                            }
                            else {
                                if ( newParent.parentNode.parentNode == null) {
                                    to = '.';
                                }
                                else if ( /\/[^\/]+$/.test(to) ) {
                                    if (newParent.raw.leaf == undefined){
                                        to = to.replace(/\/[^\/]+$/,"");
                                    }
                                    else {
                                        //to = to.replace(/\/[^\/]+\/[^\/]+$/,"");
                                        to = to.replace(/\/[^\/]+$/,"");
                                    }
                                }
                            }
                        }

                        Ext.Ajax.request({
                            url       : FrontSuite.options.apps_urldir + '/sitefront/front-framework/api.php/service/ext/filetree',
                            params    : { op : 'move', from : from, to : to },
                            method    : 'GET',
                            autoAbort : false,
                            success : function(response){
                                if (Ext.decode(response.responseText).success){
                                    //node.expand(); // TODO: get this to render correctly on reload
                                    // TODO: update moved node such that we can remove the need for a reload
                                    storeExpandedDocumentTreeNodes();
                                    this.store.reload();
                                }
                                else {
                                    return false;
                                }
                            },
                            failure: function(response, opts) {
                                Ext.MessageBox.show({
                                   title   : 'Status',
                                   msg     : 'server-side failure with status code ' + response.status,
                                   buttons : Ext.MessageBox.OK
                                });
                                return false;
                            }
                        });

                    //}
                }
            }
        }
    }
});

